// ===================================================
// View!DB
// ===================================================
function viewdb()
{
	this.url = null;
	this.send = new Object();
	this.serializeSend = null;
	this.aFields = new Array();
	this.onLoad = function(){};

	// save	private method
	this._save = function()
	{
		// build send object
		this._buildSend();
		
		// Listen to load event	
		// todo: the json object must be instanciate only once
		this.json = new BiJsonLoader();
		this.json.addEventListener("load", this.onLoad, this.json);
		
		this.json.load(this.url + "?viewdb=" + this.serializeSend);
	}
	
	// build send object
	this._buildSend = function()
	{
		var aFields = this.aFields;
		
		for (var i = 0; i < aFields.length; i++) 
		{
			var oField = application.getComponentById(aFields[i]);
			
			// todo: if it's combo use getUserValue
			eval('this.send.' + aFields[i] + '= oField.getText()');
			oField.setText('');
		}		

		// serialize output
		this.serializeSend = BiJson.serialize(this.send);
	}
	
	this.save2 = function (callbak)
	{
		this.serializeSend = BiJson.serialize(this.send);
		
		this.json = new BiJsonLoader();
		this.json.addEventListener("load", function(e)
		{
			// none for now				
		}, this.json);
		
		this.json.load(this.url + "?viewdb=" + this.serializeSend);		
	}
}

// Users
function viewdbUsers()
{
	viewdb.call(this);
	
	// Parasitic ?
	//this.Parasitic = new viewdb();
	this.aFields = new Array('vUseName', 'vUseEmail', 'vUseCity', 'vUsePassword');
	this.url = "register.php";
	this.onLoad = function()
	{
		//var oData = this.getData();

		var oWin = application.getComponentById('viewRegister');
		//oWin.setVisible(false);
		oWin.close();

		_p.generateMenu();
	}

	this.save = function()
	{
		// validations
		var sUseEmail = application.getComponentById('vUseEmail').getText();
		var sUsePassword = application.getComponentById('vUsePassword').getText(); 
		var sUsePasswordRetype = application.getComponentById('vUsePasswordRetype').getText(); 

		if (sUseEmail == '' || sUsePassword == '')
		{
			alert("Please complete the required fields!");
		}
		else
		{
			if (sUsePassword != sUsePasswordRetype)
			{
				alert("Passwords not match!");
			}
			else
			{
				//this.Parasitic.save();
				this._save();	
				alert("Welcome to BeDesk! Now you can have your own menu with your favorites links in a multiple windows enviroment. Go to My menu and click in Add folder or Add Link to build your menu. You can contribute to create the social menu too adding the appropriates links.");
			}
		}
	}
	
	this.login = function()
	{
		// redefined for login
		// todo: the names field in all form must be the same as the table
		// but there is a problem because the ids cannot be the same
		this.url = "login.php";
		this.aFields = new Array('vUseEmailLogin', 'vUsePasswordLogin');
		
		// build send object
		this._buildSend();
		
		// Listen to load event	
		this.json = new BiJsonLoader();
		this.json.addEventListener("load", function(e)
		{
			oData = this.getData();
			
			if (oData.AUTH_OK)
			{
				var oviewLogin = application.getComponentById("viewLogin");
				//oviewLogin.setVisible(false);
				oviewLogin.close();
				
				// clean textbox
				application.getComponentById("vUseEmailLogin").setText(null);
				application.getComponentById("vUsePasswordLogin").setText(null);
				
				_p.generateMenu();
				
			}
			else
			{
				alert("User invalid!");
			}
		}, this.json);
		
		this.json.load(this.url + "?viewdb=" + this.serializeSend);
	}
}
viewdbUsers.prototype = new viewdb();


// Links
function viewdbLinks()
{
	viewdb.call(this);
	this.onLoad = function()
	{
		/*var oData = this.getData();
		oWin.setVisible(false);

		var owwMenuDataModel = new wwMenuDataModel(oData.oMenu);
		var oMenu = application.getComponentById("mbWindows");
		owwMenuDataModel.buildModel(oMenu);*/
	}
	
	// save folder
	this.saveFolder = function()
	{
		// validations
		var sLnkName = application.getComponentById('vLnkName').getText();

		if (sLnkName == '')
		{
			alert("Please complete the required fields!");
		}
		else
		{
			this.aFields = new Array('vLnkName', 'iRelParentID');
			this.url = "folder.php";
			
			this._buildSend();
			
			this.json = new BiJsonLoader();
			this.json.addEventListener("load", function(e)
			{
				var oWin = application.getComponentById("viewFolder");
	//			var oData = this.getData();
				//oWin.setVisible(false);
				oWin.close();
			
				_p.generateMenu();
				
			}, this.json);
			this.json.load(this.url + "?viewdb=" + this.serializeSend);		
		}
	}
	
	// save link
	this.saveLink = function()
	{
		// validation
		var sLnkName = application.getComponentById('vLnkNameLink').getText();
		var sLnkURL = application.getComponentById('vURL').getText(); 

		if (sLnkName == '' || sLnkURL == '')
		{
			alert("Please complete the required fields!");
		}
		else
		{
			
			// todo: resolve the repeat textfield name to match table schema
			//var oWin = application.getComponentById("viewLink");
			this.aFields = new Array('vLnkNameLink', 'vURL', 'iRelParentIDLink');
			this.url = "link.php";	
			
			this._buildSend();
			
			this.json = new BiJsonLoader();
			this.json.addEventListener("load", function(e)
			{
				var oWin = application.getComponentById("viewLink");
	//			var oData = this.getData();
				//oWin.setVisible(false);
				oWin.close();
			
				_p.generateMenu();
				
			}, this.json);
		
			this.json.load(this.url + "?viewdb=" + this.serializeSend);
		}
	}
	
	// save link
	this.saveSocialLink = function()
	{
		// values
		var sLnkName = application.getComponentById('vSocialLnkNameLink').getText();
		var sLnkURL = application.getComponentById('vSocialURL').getText(); 
		var iSocialID = application.getComponentById('iSocialID').getText(); 

		// validation
		if (sLnkName == '' || sLnkURL == '')
		{
			alert("Please complete all the fields!");
		}
		else
		{
			this.url = "addsociallink.php";
			
			this.send.vLnkNameLink = sLnkName;
			this.send.vURL = sLnkURL;
			this.send.iSocialID = iSocialID;
			
			this.save2();
		
			var oWin = application.getComponentById("viewSocialLink");
			oWin.close();
			
			_p.generateSocialMenu();				
		}
	}
	
	// get links for the user
	this.fillLinks = function(oCombo)
	{
		this.url = "linkuser.php";	
		
		this.json = new BiJsonLoader();
		this.json.addEventListener("load", function(e)
		{
			var oData = this.getData();
			//alert(oData);
			// iterate link
			var y = 0;
			for(var p in oData)
			{
				//alert(oData[p]);
				li = new BiComboBoxItem;
				if (oData[p].vURL != '' && oData[p].vURL != null)
				{
					li.setIcon(new BiImage("images/application_xp.png"));
				}
				else
				{
					li.setIcon(new BiImage("images/folder.png"));
				}
				li.setUserValue(oData[p].iLnkID)
				li.setText(oData[p].vLnkName);
				if (y == 0)
					li.setSelected(true);
				oCombo.add(li);				
				y++;
			}
			
		}, this.json);

		this.json.load(this.url);

	}
	
	// delete link
	this.deleteLink = function()
	{
		this.url = "linkdelete.php";
		var ocDelLink = application.getComponentById("cDelLink");
		this.send.iLnkID = ocDelLink.getUserValue();
		// serialize output
		this.serializeSend = BiJson.serialize(this.send);
		
		this.json = new BiJsonLoader();
		this.json.addEventListener("load", function(e)
		{
			// hide window
			var oviewDelete = application.getComponentById("viewDelete");
			//oviewDelete.setVisible(false);
			oviewDelete.close();
			
			// regenerate Menu
			// todo: this is wrong!
			_p.generateMenu();
			

		}, this.json);
		
		this.json.load(this.url + "?viewdb=" + this.serializeSend);
	}
}

viewdbLinks.prototype = new viewdb();


// Menu
/*function viewdbMenu()
{
	viewdb.call(this);
	
	this.url = "menuuser.php";

	this.buildMenu = function(iUseID)
	{
		// object send
		this.send.iUseID = iUseID;
		
		this._buildSend();
		
		// Listen to load event	
		this.json.addEventListener("load", function(e)
		{
			var oData = this.getData();
			var oviewLogin = application.getComponentById("viewLogin");
			oviewLogin.setVisible(false);
			var owwMenuDataModel = new wwMenuDataModel(oData);
			var oMenu = application.getComponentById("mbWindows");
			owwMenuDataModel.buildModel(oMenu);

		}, this.json);
		
		this.json.load(this.url + "?viewdb=" + this.serializeSend);
	}
}
viewdbMenu.prototype = new viewdb();*/